package com.flx.at.spring.code.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author Fenglixiong
 * @Create 2021/11/3 21:45
 * @Description
 *
 * 1.寻找增强器
 * AOP会把@Before等切面中所有的特定注解的方法包装成增强器，存入缓存中去，key为beanName，value为List<Advisor>
 **/
@Aspect
@Component
public class CalculateAspect implements InitializingBean {

    @Pointcut("execution(* com.flx.at.spring.code.aop.calculate.Calculator.*(..))")
    public void pointCut(){

    }

    @Before(value = "pointCut()")
    public void methodBefore(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Before ["+methodName+"],param:"+ Arrays.toString(joinPoint.getArgs()));
    }

    @After(value = "pointCut()")
    public void methodAfter(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("After ["+methodName+"],param:"+ Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(pointcut = "pointCut()",returning = "result")
    public void methodReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("AfterReturning ["+methodName+"],param:"+ Arrays.toString(joinPoint.getArgs())+"],result:"+ result);
    }

    @AfterThrowing(value = "pointCut()")
    public void methodThrowing(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("AfterThrowing ["+methodName+"],param:"+ Arrays.toString(joinPoint.getArgs()));
    }

    @Around(value = "pointCut()")
    public Object methodAround(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Around ["+methodName+"] start...");
        Object result = joinPoint.proceed();
        System.out.println("Around ["+methodName+"] end...");
        return result;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(">>>>>>>>>>>>>>>CalculateAspect init...");
    }
}
